﻿using System.Net;
using System.Net.NetworkInformation;
using Guog.Common.API;

namespace Guog.Common.Net
{
    public class NetStatus
    {
        /// <summary>
        /// 判断本地计算机网络连接是否正常
        /// </summary>
        /// <returns></returns>
        public static bool IsConnected()
        {
            int I = 0;
            bool state = Wininet.InternetGetConnectedState(out I, 0);
            return state;
        }

        /// <summary>
        /// 测试指定IP是否能够连接
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool TestConnect(string ip)
        {
            bool result = false;
            IPAddress ipAddress;
            if (IPAddress.TryParse(ip, out ipAddress))
            {
                using (Ping ping = new Ping())
                {
                    if (ping.Send(ipAddress, 1000).Status == IPStatus.Success)
                    {
                        result = true;
                    }
                }
            }
            return result;
        }
    }
}
